package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");
        System.out.println("请输入要删除的图书名：");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int index = -1;
        int i = 0;
        int currentSize = bookList.getUsedSize();//通过调用bookList中的get语句获取userSize值
        for (i = 0; i < currentSize; i++) {
            Book tmp = bookList.getBook(i);
            //判断
            if(tmp.getName().equals(name)) {//存在记录下标
                index = i;
                break;
            }
    }
        if(i >= currentSize) {
            System.out.println("不存在该书");
            return ;
        }
        //删除操作
        for (int j = index; j < currentSize - 1; j++) {//找到尾位置
            //找到要删除书下标位置 使后一个书位置覆盖
            Book book = bookList.getBook(j + 1);//找到后一书位置 并赋值给book
            bookList.setBook(book,j);//使用set 覆盖
        }
        bookList.setBook(null,currentSize - 1);//赋值为空
        bookList.setUsedSize(currentSize - 1);//书架数-1
        System.out.println("删除成功！");
    }
}
